{template 'web/'.$cfg['floder'].'/base_menu'}
<script src="{MODULE_URL}template/resources/js/jquery-ui.min.js"></script>
<div class="top_tab"> 
  <a href="{php echo $this->createWebUrl('goods',array('op'=>'display'))}" class="layui-btn {if $operation!='display'}layui-btn-primary{/if}">商品管理</a> 
  <a href="{php echo $this->createWebUrl('goods',array('op'=>'categorydisplay'))}" class="layui-btn {if $operation!='categorydisplay'}layui-btn-primary{/if}">分类管理</a>
    <a href="{php echo $this->createWebUrl('goods',array('op'=>'printdisplay'))}" class="layui-btn {if $operation!='printdisplay'}layui-btn-primary{/if}">打印分类</a>
  <div class="secrchinput">
    <button><i class="fa fa-search"></i></button>
    <input type="text" id="txtTopupSeachStr" placeholder="输入">
  </div>
</div>
<style>
.panelbox-head i{ color:#009688;}
.panelbox-head a{ margin-right:10px}
</style>

{if $operation == 'categorydisplay'}
<div class="panelbox">
  <div class="panelbox-head">
      <A href="#" onclick="editBox(0)"><i class="fa fa-plus"></i>新增分类</A> <span style="color:#999">*拖动改变排序</span>
      {if $_W['isfounder']}
      <A href="#" onclick="copypcate()"><i class="fa fa-plus"></i>复制分类</A>
      {/if}
  </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <td>序号</td>
            <th>名称</th>
            <th>商品数量</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
        {loop $category $row}
        <tr rid="{$row['id']}">
          <td><input type="hidden" name="displayorder" value="{$row['id']}" />
            <span class="order">{$row['displayorder']}</span></td>
          <td>{$row['title']}</td>
          <td>{php echo pdo_fetchcolumn("SELECT count(*) FROM " . tablename('j_hmoney_goods') . " WHERE  pcate='".$row['id']."'");}</td>
          <td>{if $row['status']}<span class="label label-info">显示</span>{else}<span class="label label-default">不显示</span>{/if}</td>
          <td class="text-right"><a href="javascript:changecooker({$row['id']})" class="layui-btn layui-btn-normal layui-btn-small" title="批量转移打印分类"><i class="layui-icon">&#xe620;</i></a> <a href="javascript:editBox({$row['id']})" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a> <a href="javascript:deletepcate('{php echo $this->createWebUrl('goods',array('op'=>'delete','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
        </tr>
        {/loop}
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot"> </div>
</div>


<div id="editcatebox" style="display:none">
  <form id="editcateform" class="layui-form" action="" style="padding:15px">
    <input type="hidden" name="id" />
    <div class="layui-form-item">
      <input type="text" name="title" lay-verify="required" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-item">
      <input type="text" name="display" lay-verify="required" placeholder="排序" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-item">
      <input type="checkbox" name="status" value="1" checked="checked" lay-skin="switch" lay-text="显示|禁用">
    </div>
  </form>
</div>

<div class="cookerbox" style="display:none">
  <div class="cooker" style="padding:15px;">
    <div class="layui-form-item" style="margin-top:10px; padding:0 10px 0 0;">
      <label class="layui-form-label">转移到</label>
      <div class="layui-input-block">
        <select id="pcatelist" style="display:inline-block;" lay-ignore>
         {loop $cooklist $row}
          <option value="{$row['id']}">{$row['title']}</option>
         {/loop}
        </select>
      </div>
    </div>
  </div>
</div>
<div class="copybox" style="display:none">
    <div class="copyboxinfo" style="padding:15px;">
        <div class="layui-form-item" style="margin-top:10px; padding:0 10px 0 0;">
            <label class="layui-form-label">复制到</label>
            <div class="layui-input-block">
                <select id="shopidselect" style="display:inline-block;" lay-ignore>
                    {loop $shoplist $row}
                    <option value="{$row['id']}">{$row['companyname']}</option>
                    {/loop}
                </select>
            </div>
        </div>
    </div>
</div>

<script language="javascript">
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  form.render();
  form.on('submit(addcate)', function(data){
    
    return true;
  });
});
var fixHelper = function(e, ui) {    
	ui.children().each(function() {  
		$(this).width($(this).width());   
	});
	return ui;
};
$(".layui-table tbody").sortable({
	helper: fixHelper,
	axis:"y",  
	start:function(e, ui){  
		ui.helper.css({"background":"#F60"});
	},  
	stop:function(e, ui){  
		ui.item.css({"background":"#FFF"});
		updateMove(e, ui);
	},
}).disableSelection(); 

function updateMove(){
	var temp={};
	$("input[name='displayorder']").each(function(index, element) {
		var val=$(this).val();
        temp[val]=index+1;
    });
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createWebUrl('goods',array('op'=>'changecateorder'))}",{displayorder:temp},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(result.success){
			layer.msg("更新排序成功",{icon:1,time:2000});
			$(".order").each(function(index, element) {
                $(this).text(index+1);
            });
		}else{
			layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
		}
	});
}

function copypcate(){
    layer.open({
        type: 1,
        title:"复制分类",
        btn: ['确认', '关闭'],
        btn1:function(index,layero){
            index2=layer.load(1, {shade: [0.5,'#fff']});
            var shopid=$("#shopidselect").val();
            if(!shopid){
                layer.msg("请选择要复制到的店铺",{icon:1,time:2000});
                return;
            }
            $.post("{php echo $this->createWebUrl('goods',array('op'=>'copypcate'))}",{id:shopid},function(data2){
                layer.close(index2);
                if(data2.success){
                    layer.msg("复制成功",{icon:1,time:2000},function(){layer.closeAll();});
                }else{
                    layer.msg(data2.msg,{icon:2,time:2000});
                    return;
                }
            },'json');
        },
        content:$(".copybox").show(),
        end:function(){
            $(".copybox").hide();
        }
    });
}

function editBox(id){
	var index2=layer.load(1, {shade: [0.5,'#fff']});
	var til= id==0 ? "添加新分类":"修改分类";
	$("#editcateform")[0].reset();
	$.post("{php echo $this->createWebUrl('goods',array('op'=>'getcate'))}",{"id":id},function(data){
		layer.close(index2);
		if(!data.success){
			layer.msg(data.msg,{icon:1,time:2000});
			return;
		}
		$("#editcateform").find("input[name='id']").val(id);
		if(id==0){
			$("#editcateform").find("input[name='display']").val(data.num);
			$("#editcateform input[name='status']").attr("checked",true);
		}else{
			var items=data.item;
			if(parseInt(items.status)==0){
				$("#editcateform input[name='status']").attr("checked",false);
			}
			$("#editcateform").find("input[name='display']").val(items.displayorder);
			$("#editcateform").find("input[name='title']").val(items.title);
		}
		layer.open({
			type: 1,
			title:til,
			btn: ['确认', '关闭'],
			btn1:function(index,layero){
				index2=layer.load(1, {shade: [0.5,'#fff']});
				
				var param={"title":$("#editcateform").find("input[name='title']").val(),"displayorder":$("#editcateform").find("input[name='display']").val(),"status":$("#editcateform input[name='status']").is(":checked") ? 1 :0,"id":id}
				$.post("{php echo $this->createWebUrl('goods',array('op'=>'categorypost'))}",param,function(data2){
					layer.close(index2);
					if(!data2.success){
						layer.msg(data2.msg,{icon:1,time:2000});
						return;
					}
					location.reload();
				},'json');
			},
			content: $("#editcatebox").show(),
			end:function(){
				$("#editcatebox").hide();
			}
		});
	},'json');
	
}
function changecooker(pcateid){
	layer.open({
		type: 1,
		area:"400px",
		title:"移动打印分类",
		btn:['确定','关闭'],
		btn1:function(index,layero){
			var val=$("#pcatelist").val();
			var index2=layer.load(1, {shade: [0.1,'#fff']});
			$.post("{php echo $this->createWebUrl('goods',array('op'=>'changepcatecooker'))}",{pcate:pcateid,cookid:val},function(returnstr){
				layer.close(index2);
				var result=eval("("+returnstr+")");
				if(result.success){
					layer.msg("更新成功",{icon:1,time:2000});
					location.reload();
				}else{
					layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
				}
			});
			
		},
		skin: 'layui-layer-rim',
		content:$(".cookerbox").show(),
		end:function(){
			$(".cookerbox").hide();
		}
	});
}
function deletepcate(url){
    layer.confirm('删除分类将同时删除该分类下的所有商品，且不可恢复', {}, function(){
        deleteContent(url);
    });
}
</script>
{elseif $operation == 'display'}
<div class="panelbox">
<form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
  <div class="panelbox-head form-inline"> 
  	<A href="#" onclick="editBox(0)"><i class="fa fa-plus"></i> 新增</A> 
    <A href="#" onclick="batchOperation()"><i class="fa fa-log-in"></i> 导入</A> 
    <A href="#" onclick="putout()"><i class="fa fa-log-out"></i> 导出</A>

      {if $_W['isfounder']}
      <A href="#" onclick="copypcate()"><i class="fa fa-plus"></i>复制所有商品</A>
      {/if}
    <div class="pull-right form-inline">
    	<select id="pcatelist" style="display:inline-block;" lay-ignore onchange="jumptoNew()">
            <option value="">全部分类</option>
            {loop $cateAry $index $row}
            <option value="{$index}" {if $index==$_GPC['pcate'] && $_GPC['pcate']!=''} selected{/if}>{$row}</option>
            {/loop}
        </select>
        
        <select id="statuslist" style="display:inline-block;" lay-ignore onchange="jumptoNew()">
            <option value="0">全部商品</option>
            <option value="2" {if $_GPC['status1']==2}selected{/if}>上架商品</option>
            <option value="1" {if $_GPC['status1']==1}selected{/if}>下架商品</option>
        </select>
        
        <select id="shorttitle" style="display:inline-block;" lay-ignore onchange="jumptoNew()">
            <option value="0">全部商品</option>
            <option value="1" {if $_GPC['shorttitle']==1}selected{/if}>首字母异常商品</option>
        </select>
        
    </div>
  </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <th style="width:60px"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
            <th>名称</th>
            <th>分类</th>
            <th>首字母</th>
            <th>单位</th>
            <th>零售价</th>
            <th>库存</th>
              <th>口味</th>
            <th>状态</th>
            <th>打印分类</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          {loop $list $row}
          <tr>
            <td><input type="checkbox" name="goodid[]" value="{$row['id']}" lay-skin="primary"></td>
            <td><img src="{php echo tomedia($row['thumb'])}" width="30" onerror="this.src='./resource/images/nopic-small.jpg'" /> {$row['title']}</td>
            <td>{php echo $cateAry[$row['pcate']]}</td>
            <td>{$row['shorttitle']}</td>
            <td>{$row['unitname']}</td>
            <td>{$row['marketprice']}</td>
            <td>{$row['store']}</td>
              <td>{$row['taste']}</td>
            <td>{if $row['status']}<span class="label label-info">正常</span>{else}<span class="label label-default">下架</span>{/if}</td>
            <td><span class="label label-default">{php echo $cookary[$row['cooker']]}</span></td>
            <td class="text-right">
            <a href="javascript:editBox({$row['id']})" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a> 
            <a href="javascript:deleteContent('{php echo $this->createWebUrl('goods',array('op'=>'delete','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
          </tr>
          {/loop}
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot">
  <input type="hidden" name="token" value="{$_W['token']}" />
  <input type="submit" name="hole_up" value="批量上架" onclick="return contrle(this);return false" class="layui-btn layui-btn-normal" /> 
  <input type="submit" name="hole_down" value="批量下架" onclick="return contrle(this);return false" class="layui-btn layui-btn-normal" /> 
  <input type="button" value="转移分类" onclick="changeCate(this);" class="layui-btn layui-btn-normal" /> 
  <input type="button" name="hole_cooker" value="转移打印分类" onclick="changecooker();" class="layui-btn layui-btn-normal" /> 
  <input type="hidden" name="pcate" value=""/>
  <input type="submit" style="display:none;" name="hole_cate" id="hole_cate" value="转移分类" /> 
  <input type="submit" style="display:none;" id="hole_cooker" lay-submit="" lay-filter="btnsmbmitcook"  value="转移打印分类" /> 
  <div id="pageindex" class="pull-right"></div></div>
  </form>
</div>

<div id="editcatebox" style="display:none;">
  <form id="editcateform" class="layui-form layui-form-pane" action="" style="padding:15px">
    <input type="hidden" name="id" />
    <div class="row">
    	<div class="col-md-12" style="margin-bottom:10px;">
        	<div class="input-group ">
                <input type="text" name="thumb" value="" class="form-control" autocomplete="off">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" onclick="showImageDialog(this);">选择图片</button>
                </span>
            </div>
    <div class="input-group " style="margin-top:.5em;">
                <img src="./resource/images/nopic.jpg" onerror="this.src='./resource/images/nopic.jpg'; this.title='图片未找到.'" class="img-responsive img-thumbnail" width="150">
                <em class="close" style="position:absolute; top: 0px; right: -14px;" title="删除这张图片" onclick="deleteImage(this)">×</em>
            </div>
        </div>
    </div>
    <div class="row">
      <div class="col-md-6">
        <div class="layui-form-item">
          <label class="layui-form-label">名称</label>
          <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="名称" class="layui-input">
          </div>
        </div>
      </div>
      <div class="col-md-6">
        <select name="pcate" lay-ignore class="form-control">
          <option value="0" selected>未分类</option>
          {loop $category $row}
          <option value="{$row['id']}">{$row['title']}</option>
          {/loop}
        </select>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">编码/条码</label>
          <div class="layui-input-block">
            <input type="text" name="goodsn" autocomplete="off" placeholder="填写条码或者编码" class="layui-input">
          </div>
        </div>
      </div>
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">首字母</label>
          <div class="layui-input-block">
            <input type="text" name="shorttitle" autocomplete="off" placeholder="首字母" class="layui-input">
          </div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">成本价格</label>
          <div class="layui-input-block">
            <input type="text" name="productprice" autocomplete="off" placeholder="成本价格" class="layui-input">
          </div>
        </div>
      </div>
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">零售价</label>
          <div class="layui-input-block">
            <input type="text" name="marketprice" lay-verify="required" autocomplete="off" placeholder="零售价" class="layui-input">
          </div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">会员价</label>
          <div class="layui-input-block">
            <input type="text" name="memberprice"  autocomplete="off" placeholder="会员价" class="layui-input">
          </div>
        </div>
      </div>
      <div class="col-md-6" id="goodstatus"><input type="checkbox" name="status" value="1" lay-skin="switch" lay-text="上架|下架"></div>
    </div>
    <div class="row">
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">库存</label>
          <div class="layui-input-block">
            <input type="text" name="store" autocomplete="off" value="-1" placeholder="库存" class="layui-input">
          </div>
        </div>
      </div>
      <div class="col-md-6">
      	<div class="layui-form-item">
          <label class="layui-form-label">单位</label>
          <div class="layui-input-block">
            <input type="text" name="unitname"  autocomplete="off" placeholder="单位" value="份" class="layui-input">
          </div>
        </div>
      </div>
    </div>
      <div class="row">
          <div class="col-md-6">
              <div class="layui-form-item">
                  <select name="goodcooker" lay-ignore class="form-control">
                      <option value="0" selected>选择打印分类</option>
                      {loop $cooklist $row}
                      <option value="{$row['id']}">{$row['title']}</option>
                      {/loop}
                  </select>
              </div>
          </div>
          <div class="col-md-6">
              <label class="layui-form-label">口味</label>
              <div class="layui-input-block">
                  <input type="text" name="taste" autocomplete="off" placeholder="口味" class="layui-input">
              </div>
          </div>
      </div>
    <div class="row">
    	<div class="col-md-12"><Div class="pull-right"> <button class="layui-btn" lay-submit="" lay-filter="btnsubmit">保存</button></Div></Div>
    </div>
  </form>
</div>

<div id="batchOperation" style="display:none;">
  <form id="batchOperationform" class="layui-form" action="" style="padding:15px">
    <div class="layui-form-item">
      <label class="layui-form-label">上传文件</label>
      <div class="layui-input-block">
        <input type="file" name="batchoperationfile" id="batchoperationfile" lay-type="file" />
        <div class="help-block" style="color:#F00"><a href="{$_W['siteroot']}addons/j_hmoney/template/resources/tempfile/goods.xls" target="_blank">下载模板文件</a>,单次导入请不要多于1000条数据。</div>
      </div>
    </div>
    <div class="row">
    	<div class="col-md-12"><Div class="pull-right"> <button class="layui-btn" lay-submit="" lay-filter="btnupload">保存</button></Div></Div>
    </div>
  </form>
</div>

<div id="batchOperation2" style="display:none;">
  <form id="batchOperation2form" class="layui-form" action="" style="padding:15px">
    <div style="padding:10px; height:400px; overflow-y:scroll">
      <table class="layui-table">
        <thead>
          <tr>			
            <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>							
            <th>条码</th>
            <th>名称</th>
            <th>分类</th>
            <th>单位</th>
            <th>销售价格</th>
            <th>成本价格</th>
            <th>库存</th>
              <th>会员价格</th>
              <th>图片</th>
              <th>口味</th>
              <th>状态</th>
          </tr> 
        </thead>
        <tbody id="uploadmemberlist">
  
        </tbody>
      </table>
    </div>
    <div class="row">
    	<div class="col-md-12" style="margin-top:10px;"><button type="button" class="layui-btn layui-btn-danger" onclick="deleterow()" >删除</button> <button type="button" class="layui-btn" id="excelnum">0</button><Div class="pull-right"> <button class="layui-btn" lay-submit="" lay-filter="btnupload">导入</button></Div></Div>
    </div>
  </form>
</div>

<div class="cookerbox" style="display:none">
<div class="cooker" style="padding:15px;">
	<div class="layui-form-item" style="margin-top:10px; padding:0 10px 0 0;">
    <label class="layui-form-label">转移到</label><div class="layui-input-block">
    	<select id="pcatelist2" style="display:inline-block;" lay-ignore>
            {loop $cooklist $row}
            <option value="{$row['id']}">{$row['title']}</option>
            {/loop}
        </select>
    </div>
    </div>
</div>
</div>

<div class="copybox" style="display:none">
    <div class="copyboxinfo" style="padding:15px;">
        <div class="layui-form-item" style="margin-top:10px; padding:0 10px 0 0;">
            <label class="layui-form-label">复制到</label>
            <div class="layui-input-block">
                <select id="shopidselect" style="display:inline-block;" lay-ignore>
                    {loop $shoplist $row}
                    <option value="{$row['id']}">{$row['companyname']}</option>
                    {/loop}
                </select>
            </div>
        </div>
    </div>
</div>

<script language="javascript">
    var cookerAry=eval("("+'{php echo json_encode($cookary)}'+")");
function putout(){
	var pcate=$("#pcatelist").val();
	var status=$("#statuslist").val();
	window.open("{php echo $this->createWebUrl('goods',array('op'=>'output'))}&pcate="+pcate+"&status="+status);
}
function jumptoNew(){
	var pcate=$("#pcatelist option:selected").val();
	var status=$("#statuslist option:selected").val();
	var shorttitle=$("#shorttitle option:selected").val();
	location.href="{php echo $this->createWebUrl('goods',array('op'=>'display'))}&pcate="+pcate+"&status1="+status+"&shorttitle="+shorttitle;
}

layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  
  form.render();
  form.on('submit(btnupload)', function(data){
	  if(!$("#uploadmemberlist tr").size())return;
	  var tempary=[];
	  $("#uploadmemberlist tr").each(function(index, element) {
		  tempary.push($(this).attr("rid"));
	  });
	  var index2=layer.load(1, {shade: [0.5,'#fff']});
	  $.post("{php echo $this->createWebUrl('goods',array('op'=>'insertexcel'))}",{rowid:tempary.join(",")},function(returnstr){
		  layer.close(index2);
		  console.log(returnstr);
		  var result=eval("("+returnstr+")");
		  if(result.success){
			  layer.msg("导入完成，共导入<b>"+result.num+"</b>条数据,修改<b>"+result.num1+"</b>条数据",{icon:1,time:2000},function(){
				  location.reload();
			  });
		  }else{
			  layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
			  return;
		  }
	  })
	  
	  return false;
  });
  
  form.on('submit(btnsubmit)', function(data){

	  $.post("{php echo $this->createWebUrl('goods',array('op'=>'post'))}",data.field,function(result){
		  if(!result.success){
			  layer.msg(result.msg,{icon:1,time:2000});
			  return;
		  }
		  location.href="{php echo $this->createWeb('goods',array('op'=>'display'))}";
	  },'json');
	  return false;
  });
  form.on('checkbox(allChoose)', function(data){
    var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
    child.each(function(index, item){
      item.checked = data.elem.checked;
    });
    form.render('checkbox');
  });
   form.on('submit(btnsmbmitcook)', function(data){
	  $.post("{php echo $this->createWebUrl('goods',array('op'=>'changecooker'))}",data.field,function(result){
          console.log(result);
		  if(result.success){
              location.reload();
		  }else{
              layer.msg(result.msg,{icon:1,time:2000});
              return;
          }

	  });
	  return false;
  });
  
  laypage.render({
	  elem: 'pageindex'
	  ,count: parseInt("{$total}")
	  ,limit: 10
	  ,curr: parseInt("{php echo $pindex}")
	  ,jump: function(obj, first){
		  var curr = obj.curr;
		  var currpage=parseInt("{php echo $pindex}");
		  if(curr!=currpage){
			  var page=getUrlParam('page');
			  var url=window.location.search;
			  if(page==""){
				  url+="&page="+curr
			  }else{
				  url2=url.replace("&page="+page,"");
				  url=url2+"&page="+curr;
			  }
			  location.href="./index.php"+url;
			//return;
		  }
	  }
  });
});
function batchOperation(){
	layer.open({
		type: 1,
		title:"批量导入商品",
		area:'400px',
		skin: 'layui-layer-rim',
        shadeClose:true,
		content:$("#batchOperation").show(),
		success: function(layero, index){
			$("#batchOperationform")[0].reset();
			layui.use('upload', function(){
				var upload = layui.upload;
				var uploadInst = upload.render({
					elem: '#batchoperationfile'
					,url: "{php echo $this->createWebUrl('goods',array('op'=>'uploadfile'))}"
                    ,accept:"file"
                    ,exts:"xls"
					,done: function(returnstr){
						console.log(returnstr);
						var result=returnstr;
						if(result.success){
							layer.close(index);
							batchOperation2();
						}else{
							layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
							return;
						}
					}
					,error: function(){
						layer.alert("上传失败");
					}
				});
			});
		},
		end:function(){
			$("#batchOperation").hide();
		}
	});
}
function batchOperation2(){
	layer.open({
		type: 1,
		title:"批量导入",
		area:'900px',
        shadeClose:true,
		skin: 'layui-layer-rim',
		content:$("#batchOperation2").show(),
		success: function(layero, index){
			$("#uploadmemberlist").empty();
			var index2=layer.load(1, {shade: [0.5,'#fff']});
			$.post("{php echo $this->createWebUrl('goods',array('op'=>'dealxls'))}",{},function(returnstr){
				console.log(returnstr);
				var result=eval("("+returnstr+")");
				if(result.success){
					var list=result.list;
                    $("#uploadmemberlist").empty();
					for(var i=0;i <list.length;i++){
						var temp='<tr rid="'+(i+1)+'"><td><input type="checkbox" name="rowid['+(i+1)+']" value="'+(i+1)+'" lay-skin="primary"></td><td>'+list[i][0]+'</td><td>'+list[i][1]+'</td><td>'+list[i][2]+'</td><td>'+list[i][3]+'</td><td>'+list[i][4]+'</td><td>'+list[i][5]+'</td><td>'+list[i][6]+'</td><td>'+list[i][7]+'</td><td>'+list[i][8]+'</td><td>'+list[i][9]+'</td><td>'+list[i][10]+'</td></tr>';
						$("#uploadmemberlist").append(temp);
					}
					$("#excelnum").text(list.length+"条");
					layui.use(['form'], function(){
						var form = layui.form;
						form.render('checkbox'); 
					});
					layer.close(index2);
				}else{
					layer.close(index2);
					layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
					return;
				}
			});
			
		},
		end:function(){
			$("#batchOperation2").hide();
		}
	});
}
function deleterow(){
    if($("#uploadmemberlist .layui-form-checked").size()==0){
        layer.msg("请选择要删除的行",{icon:2,time:2000});
        return;
    }
    $("#uploadmemberlist .layui-form-checked").each(function(){
        $(this).parent().parent().remove();
    });
    $("#excelnum").text($("#uploadmemberlist tr").size()+"条");
}
function getUrlParam(name){
	var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)"); 
	var r =  window.location.search.substr(1).match(reg);
	var strValue = "";
	if (r!=null){
	 strValue= unescape(r[2]);
	}
	return strValue;
}
function contrle(obj){
	if($(".layui-form-checked").size()==0){
		layer.msg("请选择要处理的商品",{icon:1,time:2000});
		return false;
	}
	if(!$(obj).attr("checked")){
		layer.confirm('确认'+$(obj).val()+'？', {}, function(){
			$(obj).attr("checked",true);
			$(obj).click();
		});
		return false;
	}else{
		return true;
	}
	return false;
}
function changeCate(){
	if($(".layui-form-checked").size()==0){
		layer.msg("请选择要处理的商品",{icon:1,time:2000});
		return false;
	}
	layer.open({
		type: 1,
		title:"批量转移分类",
		btn:['确定','关闭'],
		btn1:function(index,layero){
			var val=$("#pcate2").val();
			$("#listform input[name='pcate']").val(val);
			$("#hole_cate").click();
		},
		skin: 'layui-layer-rim',
		content:'<div class="layui-form-item" style="margin-top:10px; padding:0 10px 0 0;"><label class="layui-form-label">转移到</label><div class="layui-input-block" id="holecate"></div></div>',
		success: function(layero, index){
			var html=$("select[name='pcate']").html();
			html='<select id="pcate2" lay-ignore class="form-control">'+html+'</select>';
			$("#holecate").html(html);
			
		},
		end:function(){
			
		}
	});
}
    function copypcate(){
        layer.open({
            type: 1,
            title:"复制商品",
            btn: ['确认', '关闭'],
            btn1:function(index,layero){
                index2=layer.load(1, {shade: [0.5,'#fff']});
                var shopid=$("#shopidselect").val();
                if(!shopid){
                    layer.msg("请选择要复制到的店铺",{icon:1,time:2000});
                    return;
                }
                $.post("{php echo $this->createWebUrl('goods',array('op'=>'copygood'))}",{id:shopid},function(data2){
                    layer.close(index2);
                    if(data2.success){
                        layer.msg("复制成功",{icon:1,time:2000},function(){layer.closeAll();});

                    }else{
                        layer.msg(data2.msg,{icon:2,time:2000});
                        return;
                    }
                },'json');
            },
            content:$(".copybox").show(),
            end:function(){
                $(".copybox").hide();
            }
        });
    }
function changecooker(){
	if($(".layui-form-checked").size()==0){
		layer.msg("请选择要处理的商品",{icon:1,time:2000});
		return false;
	}
	layer.open({
		type: 1,
		area:"400px",
		title:"批量转移打印分类",
		btn:['确定','关闭'],
		btn1:function(index,layero){
			var val=$("#pcatelist2").val();
			$("#listform input[name='pcate']").val(val);
			$("#hole_cooker").click();
		},
		skin: 'layui-layer-rim',
		content:$(".cookerbox").show(),
		end:function(){
			$(".cookerbox").hide();
		}
	});
}

function editBox(id){
	
	var til= id==0 ? "新增商品":"修改商品";
	$("#editcateform")[0].reset();
	layer.open({
		type: 1,
		title:til,
		area:'700px',
		skin: 'layui-layer-rim',
		content:$("#editcatebox").show(),
		success: function(layero, index){
			if(id){
				var index2=layer.load(1, {shade: [0.5,'#fff']});
				$.post("{php echo $this->createWebUrl('goods',array('op'=>'getgoods'))}",{"id":id},function(data){
					layer.close(index2);
					if(id){
						var goods=data.item;
						$("#editcatebox input[name='id']").val(id);
						$("#editcatebox select[name='pcate']").val(goods.pcate);
						$("#editcatebox input[name='goodsn']").val(goods.goodsn);
						$("#editcatebox input[name='title']").val(goods.title);
						$("#editcatebox input[name='productprice']").val(goods.productprice);
						$("#editcatebox input[name='marketprice']").val(goods.marketprice);
						$("#editcatebox input[name='memberprice']").val(goods.memberprice);
						$("#editcatebox input[name='unitname']").val(goods.unitname);
						$("#editcatebox input[name='store']").val(goods.store);
						$("#editcatebox input[name='thumb']").val(goods.thumb);
						$("#editcatebox img").attr("src",goods.thumb2);
                        $("#editcatebox input[name='taste']").val(goods.taste);
						$("#editcatebox input[name='shorttitle']").val(goods.shorttitle);
						$("#editcatebox select[name='goodcooker']").val(goods.cooker);

						if(parseInt(goods.status)){
							$("#editcatebox input[name='status']").attr("checked","true");
						}else{
							$("#editcatebox input[name='status']").removeAttr("checked");
						}
						layui.use(['form'], function(){
							form = layui.form;
							form.render();
						});
					}
				},'json');
			}
		},
		end:function(){
			$("#editcatebox").hide();
		}
	});
}

function showImageDialog(elm, opts, options) {
	$(".layui-layer,.layui-layer-shade").hide();
	require(["util"], function(util){
		var btn = $(elm);
		var ipt = btn.parent().prev();
		var val = ipt.val();
		var img = ipt.parent().next().children();
		options = {'global':false,'class_extra':'','direct':true,'multiple':false,'fileSizeLimit':5120000};
		util.image(val, function(url){
			$(".layui-layer,.layui-layer-shade").show();
			if(url.url){
				if(img.length > 0){
					img.get(0).src = url.url;
				}
				ipt.val(url.attachment);
				ipt.attr("filename",url.filename);
				ipt.attr("url",url.url);
			}
			if(url.media_id){
				if(img.length > 0){
					img.get(0).src = "";
				}
				ipt.val(url.media_id);
			}
		}, options);
	});
}

function deleteImage(elm){
	$(elm).prev().attr("src", "./resource/images/nopic.jpg");
	$(elm).parent().prev().find("input").val("");
}

</script>
{elseif $operation == 'printdisplay'}
<div class="panelbox">
<form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
  <div class="panelbox-head form-inline"> 
  	<A href="{php echo $this->createWebUrl('goods',array('op'=>'printpost'))}"><i class="fa fa-plus"></i> 新增</A> 
  </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <th>名称</th>
            <th>打印方式</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          {loop $list $row}
          <tr>
            <td>{$row['title']}</td>
            <td>{php echo _cookprint($row['ptype'])}</td>
            <td class="text-right"><a href="{php echo $this->createWebUrl('goods',array('op'=>'printpost','id'=>$row['id']))}" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a> <a href="javascript:deleteContent('{php echo $this->createWebUrl('goods',array('op'=>'printdelete','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
          </tr>
          {/loop}
        </tbody>
      </table>
    </div>
  </div>
  </form>
</div>
{elseif $operation == 'printpost'}
<style>
#btnList label{margin-bottom:10px; display:block;}
</style>
<div class="panelbox">
  <form action="" method="post" class="layui-form" enctype="multipart/form-data">
    <input type="hidden" name="id" value="{php echo $id}" />
    <div class="panelbox-head"> {if !$id}添加{else}编辑{/if} </div>
    <div class="panelbox-body">
      <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
          <input type="text" class="layui-input" lay-verify="required" name="title" value="{$item['title']}"/>
          <div class="help-block">标题</div>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">打印设备</label>
        <div class="layui-input-block">
          <select name="ptype">
              <option value="">全部分类</option>
              {loop _cookprint() $index $row}
              <option value="{$index}" {if $index==$item['ptype'] && $item['ptype']!=''}selected{/if}>{$row}</option>
              {/loop}
          </select>
          <div class="help-block">选择【本地打印机】，请在软件端设置打印的产品类型。如有其他牌子无线打印机，请与技术人员联系</div>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">打印参数</label>
        <div class="layui-input-block">
          <div id="btnList">
            {php $btnlist=json_decode($item['param'],true)}
            {php $i=0}
            {if count($btnlist)}
            {loop $btnlist $index $row}
            <label class='form-inline' style="" id="param-key[{php echo $i}]">
                <input class='form-control' name='param-key[{php echo $i}]' required value='{$index}' placeholder='键'/>
                :
                <input class='form-control' name='param-val[{php echo $i}]'  value='{$row}' placeholder='值'/>
                <a href='javascript:' onclick='delbtn(this)'><i class='fa fa-remove-circle'></i></a></label>
            {php $i++}
            {/loop}
            {/if}
            </div>
          <a href="javascript:addbtn()" class="layui-btn"><i class="icon-plus-sign-alt"></i> 添加</a>
          <div class="help-block">请按照不同无线打印机的方式，输入正确的参数。365打印机参数：deviceNo,key；飞鹅打印机参数：USER,UKEY,SN，飞印打印机：MEMBER_CODE，FEYIN_KEY，DEVICE_NO,易联云：client_id，client_secret，machine_code，msign；网口打印机：ip,值为IP地址；蓝牙打印机：com,值为COM口；本地打印机：name,值为打印机驱动名称，pagewidth，值为58mm/80mm；</div>
        </div>
      </div>
      
    </div>
    <div class="panelbox-foot">
      <input type="hidden" name="token" value="{$_W['token']}" />
      <input class="layui-btn" type="submit" lay-submit="" name="submit" lay-filter="btnsmbmit" value="提交">
    </div>
  </form>
</div>
<script>
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  
  form.on('submit(btnsmbmit)', function(data){
	  $.post("{php echo $this->createWebUrl('goods',array('op'=>'printpostdata'))}",data.field,function(result){
		  if(!result.success){
			  layer.msg(result.msg,{icon:1,time:2000});
			  return false;
		  }
		  location.reload();
	  },'json');
	  return false;
  });

});
var thisIndex=parseInt("{php echo $i}");
function delbtn(obj){
	$(obj).parent().remove();
}
function addbtn(){
	thisIndex=thisIndex+1;
	var temp="<label class='form-inline'><input class='form-control' name='param-key["+thisIndex+"]' required placeholder='键'/> : <input class='form-control' name='param-val["+thisIndex+"]' placeholder='值'/> <a href='javascript:' onclick='delbtn(this)'><i class='fa fa-remove-circle'></i></a></label></div>";
	$("#btnList").append(temp);
}
</script>
{/if}

<script>
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  form.render();
});
function deleteContent(url){
	layer.confirm('确认删除此内容？删除后将不可恢复', {}, function(){
		location.href=url;
	});
}
</script>